package cn.com.lms.business.exam.service;

import cn.com.lms.business.core.constant.ErrorEnum;
import cn.com.lms.business.exam.persistence.entity.Exercise;
import cn.com.lms.business.exam.persistence.entity.ExerciseQuestion;
import cn.com.lms.business.exam.persistence.repository.ExerciseQuestionRepository;


import com.bnzj.core.persistence.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ExerciseQuestionService extends BaseService<ExerciseQuestionRepository, ExerciseQuestion,Long> {



    public List<Map> findQuestionByExerciseId(Long exerciseId){
        return  repository.findQuestionByExerciseId(exerciseId);
    }

    public void batchSave(Exercise exercise,List<ExerciseQuestion> exerciseQuestionList){
        if (!CollectionUtils.isEmpty(exerciseQuestionList)) {
            List<ExerciseQuestion> exerciseQuestions = exerciseQuestionList.stream().map(exerciseQuestion -> {

                    exerciseQuestion.setExerciseId(exercise.getId());
                    exerciseQuestion.setCreator(exercise.getCreator());
                    exerciseQuestion.setCreateTime(exercise.getCreateTime());
                    exerciseQuestion.setCreatorName(exercise.getCreatorName());
                    exerciseQuestion.setIsDelete(false);
                    return exerciseQuestion;

            }).collect(Collectors.toList());
            super.saveAll(exerciseQuestions);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public int  deleteByExerciseId(Long exerciseId){
      return   repository.deleteByExerciseId(exerciseId);
    }
}
